package gui.advertiser;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;

import application.ArenaManager;
import domain.advertisement.AdType;
import domain.advertisement.AdTypeLeaderboard;
import domain.advertisement.AdTypeSkyscraper;
import domain.advertisement.AdvertisementScheme;

public class NewAdvertismenetGUI extends JPanel {

	// Components:
	private ImageIcon advertise;
	private JLabel lblIcon = null;
	private JRadioButton radioBtnLeaderboard;
	private JRadioButton radioBtnSkyscraper;
	private ButtonGroup buttonGroup;
	private JTextField txtDescription;
	private JSpinner durationSpinner;
	private JSpinner clickSpinner;
	private JSpinner viewsSpinner;
	// Data:
	private File selFile;

	public NewAdvertismenetGUI() {
		setVisible(true);
		setBorder(BorderFactory.createTitledBorder("New Ad"));
		setLayout(new BorderLayout(0, 0));

		JPanel btnPanel = new JPanel();
		add(btnPanel, BorderLayout.SOUTH);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Saving advertisement");
				addAdvertisement();

			}
		});
		btnPanel.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArenaManager.getInstance().startAdvertiser();
			}
		});
		btnPanel.add(btnCancel);

		JPanel iconPanel = new JPanel();
		add(iconPanel, BorderLayout.CENTER);
		iconPanel.setBorder(BorderFactory.createTitledBorder("Advertise"));
		iconPanel.setLayout(new BorderLayout(0, 0));

		txtDescription = new JTextField();
		txtDescription.setText("Description");
		iconPanel.add(txtDescription, BorderLayout.SOUTH);
		txtDescription.setColumns(10);
		txtDescription.setBorder(BorderFactory
				.createTitledBorder("Description"));

		lblIcon = new JLabel("");
		lblIcon.setIcon(null);
		iconPanel.add(lblIcon, BorderLayout.CENTER);
		
				JButton btnSelectImage = new JButton("Select Image");
				iconPanel.add(btnSelectImage, BorderLayout.NORTH);
				btnSelectImage.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						advertise = selectImage();
						lblIcon.setIcon(advertise);
						lblIcon.setBorder(BorderFactory
								.createTitledBorder("Advertise Preview"));
						lblIcon.repaint();
					}
				});

		JPanel schemePanel = new JPanel();
		schemePanel.setBorder(BorderFactory.createTitledBorder("Scheme"));
		add(schemePanel, BorderLayout.WEST);

		
		// Ad type:
		buttonGroup = new ButtonGroup();
		radioBtnLeaderboard = new JRadioButton("Leaderboard");
		radioBtnLeaderboard.setSelected(true);
		buttonGroup.add(radioBtnLeaderboard);

		radioBtnSkyscraper = new JRadioButton("Skyscraper");
		buttonGroup.add(radioBtnSkyscraper);
		
		JPanel durationPanel = new JPanel();
		JPanel clickPanel = new JPanel();

		JPanel viewPanel = new JPanel();
		viewPanel.setLayout(new BorderLayout(0, 0));
		GroupLayout gl_schemePanel = new GroupLayout(schemePanel);
		gl_schemePanel
				.setHorizontalGroup(gl_schemePanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_schemePanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_schemePanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																radioBtnLeaderboard)
														.addComponent(radioBtnSkyscraper))
										.addContainerGap(58, Short.MAX_VALUE))
						.addGroup(
								Alignment.TRAILING,
								gl_schemePanel
										.createSequentialGroup()
										.addGroup(
												gl_schemePanel
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																viewPanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																143,
																Short.MAX_VALUE)
														.addComponent(
																clickPanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																143,
																Short.MAX_VALUE)
														.addComponent(
																durationPanel,
																GroupLayout.DEFAULT_SIZE,
																143,
																Short.MAX_VALUE))
										.addContainerGap()));
		gl_schemePanel.setVerticalGroup(gl_schemePanel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_schemePanel
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(radioBtnLeaderboard)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(radioBtnSkyscraper)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(durationPanel,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(clickPanel, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(viewPanel, GroupLayout.PREFERRED_SIZE,
								45, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(29, Short.MAX_VALUE)));

		viewsSpinner = new JSpinner();
		viewsSpinner.setModel(new SpinnerNumberModel(10, 10, 500, 10));
		viewPanel.add(viewsSpinner, BorderLayout.CENTER);
		viewPanel
				.setBorder(BorderFactory.createTitledBorder("Number of Views"));

		clickPanel.setLayout(new BorderLayout(0, 0));
		clickPanel.setBorder(BorderFactory
				.createTitledBorder("Number of Clicks"));
		clickSpinner = new JSpinner();
		clickSpinner.setModel(new SpinnerNumberModel(10, 10, 200, 5));;
		clickPanel.add(clickSpinner, BorderLayout.CENTER);

		durationPanel.setLayout(new BorderLayout(0, 0));
		durationPanel.setBorder(BorderFactory.createTitledBorder("Viewable until"));
		durationSpinner = new JSpinner();
		durationSpinner.setModel(new SpinnerDateModel(new Date(1326409200000L), null, null, Calendar.WEEK_OF_MONTH));
		durationPanel.add(durationSpinner);

		schemePanel.setLayout(gl_schemePanel);
	}

	private ImageIcon reziseIcon(ImageIcon imgIcon, int width, int height) {
		return new ImageIcon(imgIcon.getImage().getScaledInstance(width,
				height, java.awt.Image.SCALE_SMOOTH));
	}

	private ImageIcon selectImage() {
		String filename = File.separator + "tmp";
		JFileChooser fc = new JFileChooser(new File(filename));
		// Show save dialog; this method does not return until the dialog is
		// closed
		fc.showSaveDialog(new JFrame());
		selFile = fc.getSelectedFile();

		if (radioBtnLeaderboard.isSelected()) {
			return reziseIcon(new ImageIcon(selFile.toString()), AdTypeLeaderboard.WIDTH, AdTypeLeaderboard.HEIGHT);
		} else if (radioBtnSkyscraper.isSelected()) {
			return reziseIcon(new ImageIcon(selFile.toString()), AdTypeSkyscraper.WIDTH, AdTypeSkyscraper.HEIGHT);
		} else {
			return null;
		}
	}

	private void addAdvertisement() {
		Date durationDate = (Date) durationSpinner.getValue();
		int clicks = (Integer) clickSpinner.getValue();
		int views = (Integer) viewsSpinner.getValue();
		String description = txtDescription.getText();
		
		ArenaManager.getInstance().addAd(
			ArenaManager.getInstance().getUser(), advertise, description,
				new AdvertisementScheme(getAdType(durationDate), durationDate, clicks, views));

		// Print ads:
		//ArenaManager.getInstance().getAdController().printAds();
	}

	/** Returns an AdType object */
	private AdType getAdType(Date durationDate) {
		if (radioBtnLeaderboard.isSelected()) {
			return new AdTypeLeaderboard(durationDate);
		} else if (radioBtnSkyscraper.isSelected()) {
			return new AdTypeSkyscraper(durationDate);
		} else {
			return null;
		}
	}
}
